//
//  RequeTool.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/11/29.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "RequeTool.h"
#import <AFNetworking.h>

@implementation RequeTool
+ (void)post:(NSString *)url
     andData:(NSDictionary *)params
 andCallback:(void (^)(id JSON))callback{
    __block  AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain",nil];
    
    [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (callback) {
            callback(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(error.code == -1009) {
            if (callback) {
                callback(nil);
            }
        } else if(error.code == -1001) {
            //网络超时
            if (callback) {
                callback(nil);
            }
            
        } else {
            if (callback) {
                callback(nil);
            }
            NSLog(@"Error: %@", error);
        }
        
    }];
}
@end
